package com.ktjy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktjy.dto.BusinessTrackRecordDTO;
import com.ktjy.mapper.TbBusinessMapper;
import com.ktjy.mapper.TbBusinessTrackRecordMapper;
import com.ktjy.pojo.TbBusiness;
import com.ktjy.pojo.TbBusinessTrackRecord;
import com.ktjy.service.ITbBusinessTrackRecordService;
import com.ktjy.utils.Constants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 商机跟进记录 服务实现类
 * </p>
 *
 * @author jjx
 * @since 2025-08-26
 */
@Service
public class TbBusinessTrackRecordServiceImpl extends ServiceImpl<TbBusinessTrackRecordMapper, TbBusinessTrackRecord> implements ITbBusinessTrackRecordService {
    @Autowired
    TbBusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    TbBusinessMapper businessMapper;

    @Override
    public IPage<TbBusinessTrackRecord> findPage(BusinessTrackRecordDTO businessTrackRecordDTO, Integer pageNo, Integer pageSize) {
        //构造分页条件
        Page page=new Page(pageNo,pageSize);
        //构造查询条件
        QueryWrapper qw=new QueryWrapper();
        if(businessTrackRecordDTO.getBusinessId()!=null){
            qw.eq("business_id",businessTrackRecordDTO.getBusinessId());
        }
        return businessTrackRecordMapper.selectPage(page,qw);
    }

    @Override
    @Transactional
    public void saveTrackRecord(BusinessTrackRecordDTO businessTrackRecordDTO) {
        //1.保存商机跟踪记录
        TbBusinessTrackRecord businessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackRecordDTO, businessTrackRecord);
        businessTrackRecordMapper.insert(businessTrackRecord);

        //2.修改商机状态为跟进中
        TbBusiness business = new TbBusiness();
        BeanUtils.copyProperties(businessTrackRecordDTO, business);
        business.setStatus(Constants.FOLLOWING); //进行中
        business.setId(businessTrackRecordDTO.getBusinessId());
        businessMapper.updateById(business);
    }
}
